jun.PenjualanWin = Ext.extend(Ext.Window, {
    title: 'Penjualan Sales',
    modez: 1,
    width: 900,
    height: 560,
    layout: 'form',
    modal: true,
    padding: 5,
    closeForm: false,
    initComponent: function () {
        this.items = [
            {
                xtype: 'form',
                frame: false,
                bodyStyle: 'background-color: #E4E4E4; padding: 10px',
                id: 'form-Penjualan',
                labelWidth: 100,
                labelAlign: 'left',
                layout: 'absolute',
                anchor: "100% 100%",
                ref: 'formz',
                border: false,
                items: [
                    {
                        xtype: "label",
                        text: "No Faktur:",
                        x: 5,
                        y: 5
                    },
                    {
                        xtype: 'uctextfield',
                        name: 'doc_ref',
                        id: 'doc_refid',
                        ref: '../doc_ref',
                        maxLength: 15,
                        x: 85,
                        y: 2,
                        height: 20,
                        width: 175,
                        readOnly: true
                    },
                    {
                        xtype: "label",
                        text: "Konsumen:",
                        x: 295,
                        y: 5
                    },
                    {
                        xtype: 'combo',
                        typeAhead: true,
                        triggerAction: 'all',
                        lazyRender: true,
                        mode: 'local',
                        minChars: 3,
                        store: jun.rztKonsumen,
                        matchFieldWidth: !1,
                        itemSelector: "div.search-item",
                        tpl: new Ext.XTemplate('<tpl for="."><div class="search-item">',
                            '<span style="float:left;width:120;font-weight: bold">{konsumen_code}</span>' +
                            '<span style="font-weight: bold">{konsumen_name}</span>', "</div></tpl>"),
                        listWidth: 350,
                        hiddenName: 'konsumen_id',
                        valueField: 'konsumen_id',
                        forceSelection: true,
                        hideTrigger: true,
                        displayField: 'konsumen_name',
                        ref: '../konsumen',
                        allowBlank: false,
                        x: 375,
                        y: 2
                    },
                    {
                        xtype: "label",
                        text: "Area:",
                        x: 625,
                        y: 2
                    },
                    {
                        xtype: "label",
                        text: "",
                        ref: '../area',
                        x: 705,
                        y: 2
                    },
                    {
                        xtype: "label",
                        text: "Tanggal:",
                        x: 5,
                        y: 35
                    },
                    {
                        xtype: 'xdatefield',
                        ref: '../tgl',
                        fieldLabel: 'tgl',
                        name: 'tgl',
                        id: 'tglid',
                        format: 'd M Y',
                        width: 175,
                        readOnly: true,
                        x: 85,
                        y: 32
                    },
                    {
                        xtype: "label",
                        text: "Alamat:",
                        x: 295,
                        y: 35
                    },
                    {
                        xtype: 'textarea',
                        ref: '../alamat',
                        name: 'alamat',
                        id: 'alamatid',
                        height: "80",
                        width: "215",
                        readOnly: true,
                        x: 375,
                        y: 32
                    },
                    {
                        xtype: "label",
                        text: "Pasar:",
                        x: 625,
                        y: 32
                    },
                    {
                        xtype: "label",
                        text: "",
                        ref: '../pasar',
                        x: 705,
                        y: 32
                    },
                    {
                        xtype: "label",
                        text: "Tempo:",
                        x: 5,
                        y: 65
                    },
                    {
                        xtype: 'xdatefield',
                        ref: '../tempo',
                        fieldLabel: 'tempo',
                        name: 'tempo',
                        id: 'tempoid',
                        format: 'd M Y',
                        width: 175,
//                        readOnly: true,
                        x: 85,
                        y: 62
                    },
                    {
                        xtype: "label",
                        text: "Sales:",
                        x: 5,
                        y: 95
                    },
                    {
                        xtype: 'combo',
                        typeAhead: true,
                        triggerAction: 'all',
                        lazyRender: true,
                        mode: 'local',
                        fieldLabel: 'salesman_id',
                        store: jun.rztSalesman,
                        forceSelection: true,
                        hiddenName: 'salesman_id',
                        valueField: 'salesman_id',
                        displayField: 'salesman_name',
                        width: 175,
                        readOnly: true,
                        ref: '../sales',
                        x: 85,
                        y: 92
                    },
                    new jun.DetilPenjualanGrid({
                        x: 5,
                        y: 125,
                        height: 285,
                        frameHeader: !1,
                        header: !1
                    }),
//                    {
//                        xtype: "label",
//                        text: "Sub Total:",
//                        x: 295,
//                        y: 425
//                    },
//                    {
//                        xtype: 'numericfield',
//                        name: 'sub_total',
//                        id: 'sub_totalid',
//                        ref: '../sub_total',
//                        maxLength: 30,
//                        readOnly: true,
//                        value: 0,
//                        x: 375,
//                        y: 422
//                    },
//                    {
//                        xtype: "label",
//                        text: "PPN:",
//                        x: 295,
//                        y: 455
//                    },
//                    {
//                        xtype: 'numericfield',
//                        name: 'ppn',
//                        id: 'ppnid',
//                        ref: '../ppn',
//                        maxLength: 30,
//                        readOnly: true,
//                        value: 0,
//                        x: 375,
//                        y: 452
//                    },
                    {
                        xtype: "label",
                        text: "Bruto:",
                        x: 295,
                        y: 425
                    },
                    {
                        xtype: 'numericfield',
                        name: 'bruto',
                        id: 'brutoid',
                        ref: '../bruto',
                        maxLength: 30,
                        readOnly: true,
                        value: 0,
                        x: 375,
                        y: 422
                    },
                    {
                        xtype: 'hidden',
                        name: 'disc',
                        id: 'discid',
                        ref: '../disc',
                        value: 0
                    },
                    {
                        xtype: "label",
                        text: "Total:",
                        x: 295,
                        y: 455
                    },
                    {
                        xtype: 'numericfield',
                        name: 'total',
                        id: 'totalid',
                        ref: '../total',
                        maxLength: 30,
                        readOnly: true,
                        value: 0,
                        x: 375,
                        y: 452
                    },
                    {
                        xtype: "label",
                        text: "Uang Muka:",
                        x: 625,
                        y: 425
                    },
                    {
                        xtype: 'numericfield',
                        name: 'uang_muka',
                        id: 'uang_mukaid',
                        ref: '../uang_muka',
                        maxLength: 30,
                        value: 0,
                        x: 705,
                        y: 422
                    },
//                    {
//                        xtype: "label",
//                        text: "BG/Cek:",
//                        x: 625,
//                        y: 455
//                    },
//                    {
//                        xtype: 'uctextfield',
//                        name: 'no_bg_cek',
//                        id: 'no_bg_cekid',
//                        ref: '../no_bg_cek',
//                        maxLength: 30,
//                        allowBlank: false,
//                        value: '-',
//                        x: 705,
//                        y: 452
//                    },
                    {
                        xtype: "label",
                        text: "Sisa Tagihan:",
                        x: 625,
                        y: 455
                    },
                    {
                        xtype: 'numericfield',
                        name: 'sisa_tagihan',
                        id: 'sisa_tagihanid',
                        ref: '../sisa_tagihan',
                        maxLength: 30,
                        readOnly: true,
                        value: 0,
                        x: 705,
                        y: 452
                    }
                ]
            }
        ];
        this.fbar = {
            xtype: 'toolbar',
            items: [
                {
                    xtype: 'button',
                    text: 'Retur',
                    hidden: false,
                    ref: '../btnRetur'
                },
                {
                    xtype: 'button',
                    text: 'Botol',
                    hidden: false,
                    ref: '../btnBotol'
                },
                {
                    xtype: 'button',
                    text: 'Save',
                    hidden: false,
                    ref: '../btnSave'
                },
                {
                    xtype: 'button',
                    text: 'Final',
                    ref: '../btnFinal'
                },
                {
                    xtype: 'button',
                    text: 'Batal',
                    ref: '../btnCancel'
                }
            ]
        };
        if (jun.rztBotol.getTotalCount() === 0) {
            jun.rztBotol.load();
        }
        jun.PenjualanWin.superclass.initComponent.call(this);
        this.btnRetur.on('click', this.onbtnReturclick, this);
        this.btnSave.on('click', this.onbtnSaveclick, this);
        this.btnBotol.on('click', this.onbtnBotolclick, this);
        this.btnFinal.on('click', this.onbtnFinalclick, this);
        this.btnCancel.on('click', this.onbtnCancelclick, this);
        this.konsumen.on('change', this.onKonsumenChange, this);
        this.uang_muka.on('change', this.updateTotal, this);
        this.on("close", this.onWinClose, this);
    },
    onWinClose: function () {
        jun.rztDetilPenjualan.removeAll();
    },
    onbtnReturclick: function () {
        if (this.modez == 0) {
            Ext.MessageBox.show({
                title: 'Warning',
                msg: "Faktur harus disimpan dulu.",
                buttons: Ext.MessageBox.OK,
                icon: Ext.MessageBox.WARNING
            });
            return;
        }
        var c = jun.rztReturJual.findExact('parent', this.id);
        var form;
        if (c == -1) {
            form = new jun.ReturJualWin();
            if (jun.rztSetorKasSales.totalLength == 0) {
                form.modez = 0
            } else {
                form.modez = 1;
            }
            var tgl = this.tgl.getValue();
            form.tgl.setValue(tgl);
            form.show();
            form.parent.setValue(this.id);
        } else {
            var retur = jun.rztReturJual.getAt(c);
            form = new jun.ReturJualWin();
            form.modez = 1;
            form.formz.getForm().loadRecord(retur);
            form.show();
//            Ext.getCmp('kas_keluarid').setValue(retur.data.kas_keluar);
            jun.rztReturJualDetil.baseParams = {
                penjualan_id: retur.data.penjualan_id
            };
            jun.rztReturJualDetil.load();
            jun.rztReturJualDetil.baseParams = {};
        }
        var konsumen = this.konsumen.getValue();
        var sales = this.sales.getValue();
        var faktur = this.doc_ref.getValue();
        form.konsumen_retur.setValue(konsumen);
        form.sales.setValue(sales);
        form.onSupplierChange();
    },
    onbtnBotolclick: function () {
        if (this.modez == 0) {
            Ext.MessageBox.show({
                title: 'Warning',
                msg: "Faktur harus disimpan dulu.",
                buttons: Ext.MessageBox.OK,
                icon: Ext.MessageBox.WARNING
            });
            return;
        }
        var c = jun.rztBotol.findExact('penjualan_id', this.id);
        var form;
        if (c == -1) {
            form = new jun.BotolWin();
            if (jun.rztReturBotol.totalLength == 0) {
                form.modez = 0
            } else {
                form.modez = 1;
            }
            form.show();
            var tgl = this.tgl.getValue();
            var sales = this.sales.getValue();
            var faktur = this.doc_ref.getValue();
            form.penjualan_id.setValue(this.id);
            form.tgl.setValue(tgl);
            form.sales.setValue(sales);
            form.no_faktur.setValue(faktur);
        } else {
            var botol = jun.rztBotol.getAt(c);
            form = new jun.BotolWin({
                modez: 1,
                id: botol.data.botol_id
            });
            form.show();
            form.formz.getForm().loadRecord(botol);
            var sales = this.sales.getValue();
            var faktur = this.doc_ref.getValue();
            form.penjualan_id.setValue(this.id);
            form.sales.setValue(sales);
            form.no_faktur.setValue(faktur);
            jun.rztBotolDetil.baseParams = {
                botol_id: botol.data.botol_id
            };
            jun.rztBotolDetil.load();
            jun.rztBotolDetil.baseParams = {};
        }
    },
    updateTotal: function () {
        jun.rztDetilPenjualan.refreshData();
    },
    onKonsumenChange: function () {
        var konsumen_id = this.konsumen.getValue();
        var konsumen = jun.getKonsumen(konsumen_id);
        var area = jun.getArea(konsumen.data.area_id);
        this.alamat.setValue(konsumen.data.address);
        this.area.setText(area.data.area_name);
        var tgl = this.tgl.getValue();
        if (this.modez == 0) {
            var tempo = tgl.add(Date.DAY, parseInt(konsumen.data.tempo));
            this.tempo.setValue(tempo);
        }
        if (konsumen.data.pasar_id != null) {
            var pasar = jun.getPasar(konsumen.data.pasar_id);
            this.pasar.setText(pasar.data.pasar_name);
        }
    },
    btnDisabled: function (status) {
        this.btnRetur.setDisabled(status);
        this.btnSave.setDisabled(status);
        this.btnBotol.setDisabled(status);
        this.btnFinal.setDisabled(status);
        this.btnCancel.setDisabled(status);
    },
    onbtnFinalclick: function () {
        this.modez = 2;
        this.saveForm(true);
    },
    saveForm: function () {
        this.btnDisabled(true);
        var urlz = 'Penjualan/create/';
        Ext.getCmp('form-Penjualan').getForm().submit({
            url: urlz,
            timeOut: 1000,
            scope: this,
            params: {
                detil: Ext.encode(Ext.pluck(
                    jun.rztDetilPenjualan.data.items, "data")),
                id: this.id,
                mode: this.modez
            },
            success: function (f, a) {
                jun.rztPenjualan.baseParams = {
                    sales: this.sales.getValue(),
                    tgl: this.tgl.getValue()
                };
                jun.rztPenjualan.reload();
                jun.rztPenjualan.baseParams = {};
                Ext.MessageBox.show({
                    title: 'Info',
                    msg: a.result.msg,
                    buttons: Ext.MessageBox.OK,
                    icon: Ext.MessageBox.INFO
                });
                Ext.getCmp('form-Penjualan').getForm().reset();
                this.btnDisabled(false);
                this.close();
            },
            failure: function (f, a) {
                switch (a.failureType) {
                    case Ext.form.Action.CLIENT_INVALID:
                        Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                        break;
                    case Ext.form.Action.CONNECT_FAILURE:
                        Ext.Msg.alert(a.response.statusText, a.response.responseText);
                        break;
                    case Ext.form.Action.SERVER_INVALID:
                        Ext.Msg.alert('Failure', a.result.msg);
                }
                this.btnDisabled(false);
            }

        });
    },
    onbtnSaveCloseClick: function () {
        this.closeForm = true;
        this.saveForm(true);
    },
    onbtnSaveclick: function () {
        this.closeForm = false;
        this.saveForm(false);
    },
    onbtnCancelclick: function () {
        this.close();
    }

});